package com.hydl.power.controller;

import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.Camera;
import com.hydl.power.service.CameraService;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/camera")
@Api(value = "CameraController", tags = "摄像头")
public class CameraController {
    @Autowired
    private CameraService cameraService;


   //@LogAnnotation(module = "摄像头",description = "摄像头list")
    @PostMapping(value = "/getAllCameraByPage")
    @ApiOperation(value = "摄像头list", produces = "application/json")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", required = false, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", required = false, dataType = "String"),
      @ApiImplicitParam(paramType = "query",name = "projectId",value = "项目id",required = false,dataType = "String"),
      @ApiImplicitParam(paramType = "query",name = "cityId",value = "区域id",required = false,dataType = "String"),
      @ApiImplicitParam(paramType = "query",name = "roomId",value = "配电房id",required = false,dataType = "String"),
      @ApiImplicitParam(paramType = "query",name = "token",value = "token",required = false,dataType = "String")
    })
    public Resp getAllCameraByPage(Integer pageNum,Integer pageSize,String projectId,String cityId,String roomId,String token){
        return Resp.success(cameraService.queryAllByPage(pageNum,pageSize,projectId,cityId,roomId,token));
    }


    @PostMapping(value = "/addCamera")
    @ApiOperation(value = "摄像头添加",produces ="application/json" )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "cityId",value = "区域id",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "projectId",value = "项目id",defaultValue = "1",required = false,dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name = "roomId",value = "配电房id",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "cameraName",value = "摄像头名称",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "cameraDeviceSerial",value = "摄像头序列号",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "validateCode",value = "摄像头验证码",required = false,dataType = "String")
    })
    public Resp addCameraRoom(String cityId,String projectId ,String roomId ,String cameraName,String cameraDeviceSerial,String validateCode){
      String data="添加失败";
        int i = cameraService.insert(cityId,projectId,roomId,cameraName,cameraDeviceSerial,validateCode);
        if (i == 1){
            return Resp.success("success");
        }
        if(i==20017){
          data="设备已经添加到该账号下";
        }
        if(i==20013){
        data="该设备已被别的账号添加";
        }
        if(i==20010){
        data="检查设备验证码是否错误";
        }
        if(i==20007){
          data="添加失败，设备不在线";
        }
        return Resp.success(String.valueOf(i),data);
    }

    @PostMapping(value = "/deleteCameraById")
    @ApiOperation(value = "摄像头删除",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "Integer"),
    })
    public Resp<Camera> delete(Integer id){
      String data="删除失败";
        int i = cameraService.deleteById(id);
        if (i == 1){
            return Resp.success("success");
        }
        if(i==10001){
          data="参数为空或格式不正确";
        }
        if(i==20002){
          data="设备不存在";
        }
        if(i==49999){
          data="接口调用异常";
        }
        return Resp.success(String.valueOf(i),data);
    }


    @LogAnnotation(module = "摄像头",description = "摄像头修改")
    @PostMapping(value = "/updateCameraById")
    @ApiOperation(value = "摄像头修改",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name = "cRoom",value = "配电房",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "videoName",value = "视频名称",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "guardType",value = "摄像头类型",defaultValue = "1",required = true,dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name = "videoAddress",value = "视频地址",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "messageAddress",value = "通讯地址",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "messageIp",value = "通讯ip地址",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "messagePort",value = "通讯端口",required = true,dataType = "String")
    })
    public Resp<Map<String,Object>> update( Integer  id ,String croom,String videoname,Integer guardtype,String videoaddress,String roomId,String accesstoken,String appsecret,String cameraSn,String vcode){
        Camera camera = new Camera();
        /*camera.setCroom(croom);
        camera.setVideoaddress(videoaddress);
        camera.setCamerasn(cameraSn);
        camera.setAppsecret(appsecret);
        camera.setAccesstoken(accesstoken);
        camera.setVcode(vcode);
        camera.setVideoname(videoname);
        camera.setGuardtype(guardtype);*/
        int i = cameraService.updateById(camera);


        if (i == 1){
            return Resp.success("success");
        }
        return Resp.success("修改失败");
    }
}
